package com.imonsoft.sqm.service.business.supplier_quality;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imonsoft.common.constant.ConstantSqm;
import com.imonsoft.common.httpModel.DataGrid;
import com.imonsoft.common.httpModel.Pager;
import com.imonsoft.common.util.StringUtil;
import com.imonsoft.sqm.pojo.basedata.Supplier;
import com.imonsoft.sqm.service.basedata.IpicTargetService;
import com.imonsoft.sqm.service.basedata.SupplierService;
import com.imonsoft.sqm.service.business.ipic_system.IPICService;
import com.imonsoft.sqm.vo.business.supplier_quality.DeliveryQualityVo;

@Service("deliveryQualityService")
public class DeliveryQualityServiceImpl implements DeliveryQualityService {

	@Autowired
	private IPICService iPICService;

	@Autowired
	private IpicTargetService ipicTargetService;

	@Autowired
	private SupplierService supplierService;

	@Override
	public DataGrid<DeliveryQualityVo> getDataGridVoBy(DeliveryQualityVo deliveryQualityVo, Pager pager) {
		
		List<DeliveryQualityVo> list = new ArrayList<DeliveryQualityVo>();
		if (!StringUtil.isBlank(deliveryQualityVo.getSupplierId())) {
			richSupplier(deliveryQualityVo, list);
			DataGrid<DeliveryQualityVo> dataGrid = new DataGrid<DeliveryQualityVo>(1, list);
			return dataGrid;
		} else {

			List<Supplier> bean = supplierService.findList(null);
			for (Supplier supplier : bean) {
				DeliveryQualityVo deliveryQualit = new DeliveryQualityVo();
				deliveryQualit.setSupplierId(StringUtil.fixup(supplier.getId()));
				deliveryQualit.setYear(deliveryQualityVo.getYear());

				richSupplier(deliveryQualit, list);
			}
			DataGrid<DeliveryQualityVo> dataGrid = new DataGrid<DeliveryQualityVo>(bean.size(), list);
			return dataGrid;
		}
	}

	private void richSupplier(DeliveryQualityVo deliveryQualityVo, List<DeliveryQualityVo> list) {
		DeliveryQualityVo vo = new DeliveryQualityVo();
		Map<String, Object> searchMap = new HashMap<String, Object>();
		searchMap.put("supplierId", Long.valueOf(deliveryQualityVo.getSupplierId()));
		searchMap.put("year", deliveryQualityVo.getYear());
		searchMap.put("type", ConstantSqm.TYPE_CLAIM);
		Double target = ipicTargetService.getIpicTargetValue(searchMap);
		Supplier supplier = supplierService.get(Long.valueOf(deliveryQualityVo.getSupplierId()));
		vo.setSupplierCode(supplier.getCode());
		vo.setSupplierName(supplier.getName());
		vo.setYear(deliveryQualityVo.getYear());
		DecimalFormat decimalFormat=new DecimalFormat(",###");//格式化类
		vo.setIpiCTarget(decimalFormat.format(target));

		
		for (int i = 1; i <= 12; i++) {
			searchMap.put("month", StringUtil.paddingZero(String.valueOf(i), 2));
			Map<String, Object> resultMap = new HashMap<String, Object>();
			iPICService.calIPICMonthData(searchMap, resultMap);
			Integer value = (Integer) resultMap.get("ipicMonth");
			if (i == 1) {
				vo.setJanuary(String.valueOf(value));
			} else if (i == 2) {
				vo.setFebruary(String.valueOf(value));
			} else if (i == 3) {
				vo.setMarch(String.valueOf(value));
			} else if (i == 4) {
				vo.setApril(String.valueOf(value));
			} else if (i == 5) {
				vo.setMay(String.valueOf(value));
			} else if (i == 6) {
				vo.setJune(String.valueOf(value));
			} else if (i == 7) {
				vo.setJuly(String.valueOf(value));
			} else if (i == 8) {
				vo.setAugust(String.valueOf(value));
			} else if (i == 9) {
				vo.setSeptember(String.valueOf(value));
			} else if (i == 10) {
				vo.setOctober(String.valueOf(value));
			} else if (i == 11) {
				vo.setNovember(String.valueOf(value));
			} else if (i == 12) {
				vo.setDecember(String.valueOf(value));
			}
		}
		list.add(vo);
	}

}
